<?php

namespace card\app;

use card\models\CardLog;
use framework\common\BasicController;
use Yii;
use yii\data\ActiveDataProvider;

class CardlogController extends BasicController
{
    /**
     * 重写父类
     * @return [type] [description]
     */
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index']);
        unset($actions['view']);
        unset($actions['create']);
        unset($actions['update']);
        return $actions;
    }

    public function actionIndex()
    {
        //获取头部信息
        $headers = Yii::$app->getRequest()->getHeaders();
        //获取分页信息
        $pageSize = $headers->get('X-Pagination-Per-Page') ?? 20;
        $AppID = Yii::$app->params['AppID'];
        $get = \Yii::$app->request->get();
        $log_type = $get['log_type'] ?? 0;
        $where = ['UID' => \Yii::$app->user->id, 'AppID' => $AppID, 'is_deleted' => 0];
        if ($log_type) {
            $where = ['and', $where, ['and', ['log_type' => $log_type]]];
        }
        $orderBy = ['id' => SORT_DESC];

        $data = new ActiveDataProvider(
            [
                'query' => CardLog::find()
                    ->where($where)
                    ->orderBy($orderBy)
                    ->asArray(),
                'pagination' => ['pageSize' => $pageSize, 'validatePage' => false],
            ]
        );
        $list = $data->getModels();
        if ($list) {
            foreach ($list as &$item) {
                unset($item['is_refunded']);
                unset($item['refund_amount']);
            }
        }

        $list = str2url($list);
        $data->setModels($list);
        return $data;
    }
}
